package mysql

import (
	"gorm.io/gorm"
	"manuscript-service/manuscrpc/manuscrpc"
)

type Manuscript struct {
	gorm.Model
	Title   string `gorm:"type:varchar(30);not null;comment:标题"`
	Like    int64  `gorm:"type:int(11);not null;comment:点赞数"`
	Content string `gorm:"type:varchar(255);not null;comment:内容"`
	ClassId int64  `gorm:"type:int(11);not null;comment:类型id"`
}

func (m *Manuscript) ManuscriptAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Manuscript) FindManusByTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("title=?", title).First(m).Error
}
func ManuscriptList(db *gorm.DB) []*manuscrpc.ManuscriptList {
	/*
		SELECT
		  manuscripts.id AS manuscriptId,
		  manuscripts.title AS title,
		  manuscripts.`like` AS `like`,
		  manuscripts.content AS content,
		  classes.`name` AS className
		FROM
		  manuscripts
		  LEFT JOIN classes ON manuscripts.class_id = classes.id
	*/
	var mancs []*manuscrpc.ManuscriptList
	db.Debug().Model(&Manuscript{}).
		Select(
			"manuscripts.id AS manuscript_id",
			"manuscripts.title AS title",
			"manuscripts.`like` AS `like`",
			"manuscripts.content AS content",
			"classes.`name` AS class_name",
		).
		Joins("LEFT JOIN classes ON manuscripts.class_id = classes.id").
		Find(&mancs)
	return mancs

}
